package org.xshop.core;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;

import top.ibase4j.core.exception.BaseException;
import top.ibase4j.core.support.HttpCode;
import top.ibase4j.core.support.Token;
import top.ibase4j.core.util.CacheUtil;

public class ExpressmanHelper {

    @SuppressWarnings("serial")
    public static Long getCurrent(HttpServletRequest request) {
        String uuid = request.getHeader("UUID");
        Assert.notNull(uuid, "非法操作.");

        String tokenJson = (String)CacheUtil.getCache().hget("S:iBase4J:EXPRESSMAN:TOKENS", uuid);
        if (tokenJson == null) {
            throw new BaseException() {
                @Override
                protected HttpCode getCode() {
                    return HttpCode.UNAUTHORIZED;
                }
            };
        }

        Token token = JSON.parseObject(tokenJson, Token.class);
        return Long.parseLong(token.getValue());
    }
}
